<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="auth" uri="auth"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link rel="stylesheet" href="${ctx}/css/common.css" />
<link rel="stylesheet" href="${ctx}/css/pagination.css" />

<script type="text/javascript" src="${ctx}/js/jquery.js"></script>
<script type="text/javascript" src="${ctx}/js/jquery.pagination.js"></script>

<script language="javascript">
<c:if test="${action == 'del'}">
ymPrompt.succeedInfo("<s:text name='DelAccountSuccess' />！",null,null,null,null);
</c:if>

var pageIndex = 0;
var pageSize = 10;

$(document).ready(function() {
    //分页
    InitTable(0);
    
    $("#accountPage").pagination(${accountCount}, {
            callback: pageCallback,  
            prev_text: '<s:text name="PrePage" />',       //上一页按钮里text  
            next_text: '<s:text name="NextPage" />',       //下一页按钮里text  
            items_per_page: pageSize,  //显示条数  
            num_display_entries: 6,    //连续分页主体部分分页条目数  
            current_page: pageIndex,   //当前页索引  
            num_edge_entries: 2        //两侧首尾分页条目数  
    });
    
    //翻页调用  
    function pageCallback(index,jq) {
    	InitTable(index);
    }
    
    //初始化表格
    function InitTable(pageIndex) {
    	top.showProgress("<s:text name='LoadingAccountData' />....");
    	
    	if($("#accountName").val() == ""){
    		var ajaxUrl = "../ajax/getAccountByPage.action?page="+pageIndex+"&pageSize="+pageSize;     //提交到一般处理程序请求数据
    	} else {
    		var ajaxUrl = "../ajax/getAccountByPage.action?page="+pageIndex+"&pageSize="+pageSize +
				"&search=" + encodeURI(encodeURI($("#accountName").val())) + "&t=" + Math.random();     //提交到一般处理程序请求数据
    	}

    	$("#dataList tbody").html("");
        
        $.getJSON(ajaxUrl,function(data){
        	html = "";
        	
        	if(data == null){
        		html += "<tr>\r\n"+
							"<td height='40' colspan='5' class='td_content'><s:text name='HasNoAccountData' /></td>\r\n"+
						"</tr>\r\n";
        	} else {
        		$.each(data,function(id,value){
                	html += "<tr onmouseover=\"this.style.background='#F2F2F2'\" onmouseout=\"this.style.background='#EEF7FD'\">\r\n"+
		                	"<td class=\"td_content\">"+value.id+"</td>"+
		                    "<td class=\"td_content\">"+value.app.appName+"</td>"+
		                    "<td class=\"td_content\">"+value.name+"</td>"+
		                    "<td class=\"td_content\">"+value.date+"</td>";
		                    
		            
        			html += "<td class='td_content'>"+
        					"<auth:entity entityCode='UPDATE_ACCOUNT'><a href='javascript:updateAccount("+value.id+")'><s:text name='UpdateAccount' /></a> | </auth:entity>";
        			
        			if(value.id == $("#currentAccountId").val()){
        				html += "<a href='javascript:updatePassword("+value.id+")'><s:text name='UpdatePassword' /></a> | ";
        			} else {
        				html += "<auth:entity entityCode='RESET_PASSWORD'><a href='javascript:resetPassword("+value.id+")'><s:text name='ResetPassword' /></a> | </auth:entity>";
        			}
        				
        			html += "<auth:entity entityCode='ASSIGNMENT_AUTHS'><a href='javascript:assignmentAuths("+value.id+")'><s:text name='AssignmentAuths' /></a> | </auth:entity>"+
        					"<auth:entity entityCode='DELETE_ACCOUNT'><a href='javascript:del("+value.id+");'><s:text name='Delete' /></a></auth:entity></td>\r\n"+
        			   		"</tr>\r\n";
                });
        	}
        	
    		$("#dataList tbody").append(html);
    		
    		top.hideProgress();
        });                  
    }
});

function del(id){
	parent.ymPrompt.confirmInfo("<s:text name='ConfirmToDeleteThisAccount' />？",null,
			null,null,
			function(tp){
				if(tp=="ok"){
					window.location.href="delAccount.action?id="+id;
				}
			}
	);
}

function addAccount(){
	parent.ymPrompt.win('editAccountPage.action',500,300,'<s:text name="AddAccount" />',null,null,null,{id:'editAccountWin'});
}

function updateAccount(id) {
	parent.ymPrompt.win('editAccountPage.action?id='+id,500,300,'<s:text name="UpdateAccount" />',null,null,null,{id:'editAccountWin'});
}

function updatePassword(id){
	parent.ymPrompt.win('updatePasswordPage.action?id='+id,500,300,'<s:text name="UpdatePassword" />',null,null,null,{id:'editAccountWin'});
}

function assignmentAuths(id){
	parent.ymPrompt.win("assignmentAuthsPage.action?accountId="+id,600,500,"<s:text name='AssignmentAuths' />",null,null,null,{id:'editAccountWin'});
}

/**
 * 重置密码
 */
function resetPassword(id) {
	top.showProgress("<s:text name='LoadingResetPassword' />....");
	var ajaxUrl = "../ajax/resetPassword.action?accountid=" + id;
	$.getJSON(ajaxUrl,function(data){
		top.hideProgress();
		if(data == "1") {
			parent.ymPrompt.succeedInfo("<s:text name='ResetPasswordSuccess' />！");
		} else {
			parent.ymPrompt.succeedInfo("<s:text name='ResetPasswordFails' />！");
		}
	});
}
</script>

</head>

<body>
	<input id="currentAccountId" name="currentAccountId" type="hidden" value="${currentAccountId }" />
	<div class="table_header">
		<span class="search_bar"><s:text name="SearchAccount" /></span>
	</div>
	<div class="table_content">
		<form action="searchAccount.action" method="post">
			<span style="padding-left:10px;"><s:text name="AccountName" /></span>：
			<input id="accountName" name="accountName" type="text" value="${accountName }" />
			<input type="submit" value="<s:text name="Search" />" />
		</form>
	</div>
	<div class="clear"></div>
	<div class="table_header">
		<auth:entity entityCode='ADD_ACCOUNT'>
    	<a href="javascript:addAccount();">
    		<span class="create_btn"><s:text name="AddAccount" /></span>
        </a>
        </auth:entity>
    </div>
	<table id="dataList" width="100%" cellspacing="1" cellpadding="5" class="table_class">
    	<thead>
        	<tr>
            	<td class="td_header">ID</td>
                <td class="td_header"><s:text name="AppName" /></td>
                <td class="td_header" width="60%"><s:text name="AccountName" /></td>
                <td class="td_header"><s:text name="RegDate" /></td>
                <td class="td_header"><s:text name="Operate" /></td>
            </tr>
        </thead>
        <tbody>
        </tbody>
        <tfoot>
	        <tr>
				<td colspan="5" class="td_content">
					<div id="accountPage" class="pagination"></div>
				</td>
			</tr>
        </tfoot>
    </table>
</body>
</html>
